package com.henu.zmb;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;

import com.henu.R;
import com.henu.zmb.adapter.GoodsClass_adapter;
import com.henu.zmb.adapter.Goods_adapter;
import com.henu.zmb.entity.Goods;
import com.henu.zmb.entity.GoodsClass;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobPointer;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class Goods_MainActivity extends AppCompatActivity {
    ListView lg;
    ListView lgc;
    Goods_adapter g_adapter;
    GoodsClass_adapter gc_adapter;
    ArrayList<Goods> goods = new ArrayList<Goods>();
    ArrayList<GoodsClass> goodsclass = new ArrayList<GoodsClass>();
    private ImageButton agm_return;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods__main);
        lgc = (ListView)findViewById(R.id.agm_lvgoodsclass);
        gc_adapter = new GoodsClass_adapter(this);
        lgc.setAdapter(gc_adapter);
        Bmob.initialize(this, "a24db0fac16b375a97072b85080ea20f");
        // 写入类别数据
        BmobQuery<GoodsClass> query = new BmobQuery<GoodsClass>();
        //返回50条数据，如果不加上这条语句，默认返回10条数据
        query.setLimit(50);
        //执行查询方法
        query.findObjects(new FindListener<GoodsClass>() {
            @Override
            public void done(List<GoodsClass> object, BmobException e) {
                if(e==null){
                    goodsclass=(ArrayList)object;
                    gc_adapter.addData(goodsclass); // 绑定数据
                    gc_adapter.notifyDataSetChanged();// 显示
                }else{
                    System.out.println("bmob失败："+e.getMessage()+","+e.getErrorCode());
                }
            }
        });
        lg = (ListView)findViewById(R.id.agm_lvgoods);
        g_adapter = new Goods_adapter(this,false);
        lg.setAdapter(g_adapter);
        // 类别 点击事件
        lgc.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 写入数据
                goods.clear();
                BmobQuery<Goods> query = new BmobQuery<Goods>();
                GoodsClass gc=new GoodsClass();
                gc.setObjectId(goodsclass.get(position).getObjectId());
                // 通过类别id查找当前分类下的所有物品
                query.addWhereEqualTo("GoodsClass_Id", new BmobPointer(gc));
                query.include("User_Id");
                query.setLimit(50);
                //执行查询方法
                query.findObjects(new FindListener<Goods>() {
                    @Override
                    public void done(List<Goods> object, BmobException e) {
                        if(e==null){
                            goods=(ArrayList)object;
                            g_adapter.addAllData(goods); // 绑定数据
                            g_adapter.notifyDataSetChanged();// 显示
                        }else{
                            System.out.println("bmob失败："+e.getMessage()+","+e.getErrorCode());
                        }
                    }
                });
            }
        });
        // 物品 点击事件
        lg.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(Goods_MainActivity.this,GoodsDetails_Activity.class); // 设置跳转的路径
                Goods good = goods.get(position); // 取出内容
                intent.putExtra("good", good); // 需要实例化Goods
                startActivity(intent); // 跳转
            }
        });
        agm_return = (ImageButton) findViewById(R.id.agm_return);
        agm_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


    }
}
